//基本设置（页面的配置）

(function ($) {
    "use strict";

    // Variables declarations

    var $wrapper = $('.main-wrapper');
    var $pageWrapper = $('.page-wrapper');
    var $slimScrolls = $('.slimscroll');

    // Sidebar

    var Sidemenu = function () {
        this.$menuItem = $('#sidebar-menu a');
    };

    function init() {
        var $this = Sidemenu;
        $('#sidebar-menu a').on('click', function (e) {
            if ($(this).parent().hasClass('submenu')) {
                e.preventDefault();
            }
            if (!$(this).hasClass('subdrop')) {
                $('ul', $(this).parents('ul:first')).slideUp(350);
                $('a', $(this).parents('ul:first')).removeClass('subdrop');
                $(this).next('ul').slideDown(350);
                $(this).addClass('subdrop');
            } else if ($(this).hasClass('subdrop')) {
                $(this).removeClass('subdrop');
                $(this).next('ul').slideUp(350);
            }
        });
        $('#sidebar-menu ul li.submenu a.active').parents('li:last').children('a:first').addClass('active').trigger('click');
    }

    // Sidebar Initiate
    init();

    // Mobile menu sidebar overlay

    $('body').append('<div class="sidebar-overlay"></div>');
    $(document).on('click', '#mobile_btn', function () {
        $wrapper.toggleClass('slide-nav');
        $('.sidebar-overlay').toggleClass('opened');
        $('html').addClass('menu-opened');
        return false;
    });

    // Sidebar overlay

    $(".sidebar-overlay").on("click", function () {
        $wrapper.removeClass('slide-nav');
        $(".sidebar-overlay").removeClass("opened");
        $('html').removeClass('menu-opened');
    });

    // Page Content Height

    if ($('.page-wrapper').length > 0) {
        var height = $(window).height();
        $(".page-wrapper").css("min-height", height);
    }

    // Page Content Height Resize

    $(window).resize(function () {
        if ($('.page-wrapper').length > 0) {
            var height = $(window).height();
            $(".page-wrapper").css("min-height", height);
        }
    });

    // Select 2

    if ($('.select').length > 0) {
        $('.select').select2({
            minimumResultsForSearch: -1,
            width: '100%'
        });
    }

    // Datetimepicker

    if ($('.datetimepicker').length > 0) {
        $('.datetimepicker').datetimepicker({
            format: 'DD-MM-YYYY',
            icons: {
                up: "fas fa-angle-up",
                down: "fas fa-angle-down",
                next: 'fas fa-angle-right',
                previous: 'fas fa-angle-left'
            }
        });
        $('.datetimepicker').on('dp.show', function () {
            $(this).closest('.table-responsive').removeClass('table-responsive').addClass('temp');
        }).on('dp.hide', function () {
            $(this).closest('.temp').addClass('table-responsive').removeClass('temp')
        });
    }

    // Tooltip

    if ($('[data-toggle="tooltip"]').length > 0) {
        $('[data-toggle="tooltip"]').tooltip();
    }

    // Datatable

    if ($('.datatable').length > 0) {
        $('.datatable').DataTable({
            "bFilter": false,
        });
    }

    // Check all email

    $(document).on('click', '#check_all', function () {
        $('.checkmail').click();
        return false;
    });
    if ($('.checkmail').length > 0) {
        $('.checkmail').each(function () {
            $(this).on('click', function () {
                if ($(this).closest('tr').hasClass('checked')) {
                    $(this).closest('tr').removeClass('checked');
                } else {
                    $(this).closest('tr').addClass('checked');
                }
            });
        });
    }

    // Mail important

    $(document).on('click', '.mail-important', function () {
        $(this).find('i.fa').toggleClass('fa-star').toggleClass('fa-star-o');
    });

    // Summernote

    if ($('.summernote').length > 0) {
        $('.summernote').summernote({
            height: 200,                 // set editor height
            minHeight: null,             // set minimum height of editor
            maxHeight: null,             // set maximum height of editor
            focus: false                 // set focus to editable area after initializing summernote
        });
    }


    // Sidebar Slimscroll

    if ($slimScrolls.length > 0) {
        $slimScrolls.slimScroll({
            height: 'auto',
            width: '100%',
            position: 'right',
            size: '7px',
            color: '#ccc',
            allowPageScroll: false,
            wheelStep: 10,
            touchScrollStep: 100
        });
        var wHeight = $(window).height() - 60;
        $slimScrolls.height(wHeight);
        $('.sidebar .slimScrollDiv').height(wHeight);
        $(window).resize(function () {
            var rHeight = $(window).height() - 60;
            $slimScrolls.height(rHeight);
            $('.sidebar .slimScrollDiv').height(rHeight);
        });
    }

    // Small Sidebar

    $(document).on('click', '#toggle_btn', function () {
        if ($('body').hasClass('mini-sidebar')) {
            $('body').removeClass('mini-sidebar');
            $('.subdrop + ul').slideDown();
        } else {
            $('body').addClass('mini-sidebar');
            $('.subdrop + ul').slideUp();
        }
        setTimeout(function () {
            mA.redraw();
            mL.redraw();
        }, 300);
        return false;
    });
    $(document).on('mouseover', function (e) {
        e.stopPropagation();
        if ($('body').hasClass('mini-sidebar') && $('#toggle_btn').is(':visible')) {
            var targ = $(e.target).closest('.sidebar').length;
            if (targ) {
                $('body').addClass('expand-menu');
                $('.subdrop + ul').slideDown();
            } else {
                $('body').removeClass('expand-menu');
                $('.subdrop + ul').slideUp();
            }
            return false;
        }
    });

})(jQuery);

//-------------------------------------------------
//发送ajax请求并处理

function sendAjax(url, type, data, func, button) {
    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function (res) {
            handleResult(res, func);
        },
        error: function (e) {
            alert('系统异常，请稍后重试。');
        },
        beforeSend: function (xhr) {
            beforeSend(xhr, button)
        },
        complete: function (xhr, text) {
            complete(xhr, text, button)
        }
    });
}

function handleResult(res, func) {
    if (res.code === 0) {
        func(res);
    } else {
        alert(res.msg);
    }
}

function beforeSend(xhr, button) {
    $(button).attr("disabled", "disabled");
}

function complete(xhr, text, button) {
    $(button).removeAttr("disabled");
}


function uploadFile(url, type, data, func, button) {
    $.ajax({
        url: url,
        type: type,
        data: data,
        async: true,
        cache: false,
        contentType: false,
        processData: false,
        success: function (res) {
            handleResult(res, func);
        },
        error: function (e) {
            alert('系统异常，请稍后重试。');
        },
        beforeSend: function (xhr) {
            beforeSend(xhr, button)
        },
        complete: function (xhr, text) {
            complete(xhr, text, button)
        }
    });
}

//----------------------------------------------------------------------
//分页相关

function changeLimit(allids) {
    currentPage = 1;
    $("#offset").val(0);
    $("#deleteCheckBtn").prop("disabled", "disabled");
    $(".checkall").prop("checked", false);
    allids = [];
    myrefresh();
    return allids;
}

function paging(res) {
    limit = $("#limit option:selected").val();
    begin = Number($("#offset").val()) + 1;
    end;
    if (Number(begin) + Number(limit) > Number(res.total)) {
        end = res.total;
    } else {
        end = Number(begin) + Number(limit) - 1;
    }
    $("#begin").text(begin);
    $("#end").text(end);
    $("#sum").text(res.total);

    pageNum = Math.ceil(Number(res.total) / Number(limit));
    if (pageNum > 1) {
        var previous;
        if (currentPage === 1) {
            previous = '<li class="page-item disabled" id="previous">\n' +
                '                <a class="page-link" href="#" aria-label="Previous" onclick="newPage(' + (currentPage - 1) + ')">\n' +
                '                    <span aria-hidden="true">&laquo;</span>\n' +
                '                </a>\n' +
                '           </li>';
        } else {
            previous = '<li class="page-item" id="previous">\n' +
                '                <a class="page-link" href="#" aria-label="Previous" onclick="newPage(' + (currentPage - 1) + ')">\n' +
                '                    <span aria-hidden="true">&laquo;</span>\n' +
                '                </a>\n' +
                '           </li>';
        }
        var next;
        if (currentPage === pageNum) {
            next = '<li class="page-item disabled" id="next">\n' +
                '            <a class="page-link" href="#" aria-label="Next" onclick="newPage(' + (currentPage + 1) + ')">\n' +
                '                 <span aria-hidden="true">&raquo;</span>\n' +
                '            </a>\n' +
                '       </li>';
        } else {
            next = '<li class="page-item" id="next">\n' +
                '            <a class="page-link" href="#" aria-label="Next" onclick="newPage(' + (Number(currentPage) + 1) + ')">\n' +
                '                 <span aria-hidden="true">&raquo;</span>\n' +
                '            </a>\n' +
                '       </li>';
        }
        var html = previous;
        if (pageNum <= 7) {
            for (var i = 1; i <= pageNum; i++) {
                if (i === Number(currentPage)) {
                    html += '<li class="page-item active" id="page' + i + '"><a class="page-link" onclick="newPage(' + i + ')">' + i + '</a></li>';
                } else {
                    html += '<li class="page-item" id="page' + i + '"><a class="page-link" onclick="newPage(' + i + ')">' + i + '</a></li>';
                }
            }
        } else {
            if (currentPage < 5) {
                for (var i = 1; i <= 5; i++) {
                    if (i === Number(currentPage)) {
                        html += '<li class="page-item active" id="page' + i + '"><a class="page-link" onclick="newPage(' + i + ')">' + i + '</a></li>';
                    } else {
                        html += '<li class="page-item" id="page' + i + '"><a class="page-link" onclick="newPage(' + i + ')">' + i + '</a></li>';
                    }
                }
                html += '<li class="page-item disabled"><a class="page-link">...</a></li>';
                html += '<li class="page-item" id="page' + pageNum + '"><a class="page-link" onclick="newPage(' + pageNum + ')">' + pageNum + '</a></li>';
            } else if (Number(pageNum) - Number(currentPage) < 4) {
                html += '<li class="page-item" id="page1"><a class="page-link" onclick="newPage(1)">1</a></li>';
                html += '<li class="page-item disabled"><a class="page-link">...</a></li>';
                for (var i = Number(pageNum) - 4; i <= Number(pageNum); i++) {
                    if (i === Number(currentPage)) {
                        html += '<li class="page-item active" id="page' + i + '"><a class="page-link" onclick="newPage(' + i + ')">' + i + '</a></li>';
                    } else {
                        html += '<li class="page-item" id="page' + i + '"><a class="page-link" onclick="newPage(' + i + ')">' + i + '</a></li>';
                    }
                }
            } else {
                html += '<li class="page-item" id="page1"><a class="page-link" onclick="newPage(1)">1</a></li>';
                html += '<li class="page-item disabled"><a class="page-link">...</a></li>';
                html += '<li class="page-item" id="page' + (Number(currentPage) - 1) + '"><a class="page-link" onclick="newPage(' + (Number(currentPage) - 1) + ')">' + (Number(currentPage) - 1) + '</a></li>';
                html += '<li class="page-item active" id="page' + Number(currentPage) + '"><a class="page-link" onclick="newPage(' + Number(currentPage) + ')">' + Number(currentPage) + '</a></li>';
                html += '<li class="page-item" id="page' + (Number(currentPage) + 1) + '"><a class="page-link" onclick="newPage(' + (Number(currentPage) + 1) + ')">' + (Number(currentPage) + 1) + '</a></li>';
                html += '<li class="page-item disabled"><a class="page-link">...</a></li>';
                html += '<li class="page-item" id="page' + pageNum + '"><a class="page-link" onclick="newPage(' + pageNum + ')">' + pageNum + '</a></li>';
            }
        }
        html += next;
        $("#pages").html(html);
    } else {
        $("#pages").html("");
    }
}

function newPage(page) {
    this.allNoticeIds = [];
    this.allTeacherIds = [];
    this.allStudentIds = [];
    this.allCourseIds = [];
    this.allClassIds = [];
    $("#deleteCheckBtn").prop("disabled", "disabled");
    $(".checkall").prop("checked", false);
    currentPage = page;
    $("#offset").val((Number(page) - 1) * Number($("#limit option:selected").val()));
    myrefresh();
}

//----------------------------------------------------------------------
//表格相关

function hiddenTD(col) {
    let id = "#" + col;
    if ($(id).prop("checked")) {
        $("." + col).removeAttr("hidden");
    } else {
        $("." + col).attr("hidden", "hidden");
    }
}

function deleteallcheck(selectStatus, alldeleteIds) {
    if (selectStatus) {
        $("#deleteCheckBtn").removeAttr("disabled");
        $(".checks").each(function (i, n) {
            n.checked = true;
            var id = $(this).parent().parent().children(".td1").text();
            if ($.inArray(Number(id), alldeleteIds) < 0) {
                alldeleteIds.push(Number(id));
            }
        });
    } else {
        alldeleteIds = [];
        $(".checks").each(function (i, n) {
            n.checked = false;
        });
        $("#deleteCheckBtn").prop("disabled", "disabled");
    }
    return alldeleteIds;
}

function deletecheck(id, alldeleteIds) {
    if ($.inArray(Number(id), alldeleteIds) < 0) {
        alldeleteIds.push(id);
    } else {
        alldeleteIds.splice($.inArray(Number(id), alldeleteIds), 1);
    }
    if ($(".checks:not(:checked)").length > 0) {
        $(".checkall").prop("checked", false);
    } else {
        $(".checkall").prop("checked", true);
    }
    if ($(".checks:checked").length > 0) {
        $("#deleteCheckBtn").removeAttr("disabled");
    } else {
        $("#deleteCheckBtn").prop("disabled", "disabled");
    }
    return alldeleteIds;
}


//--------------------------------------------------------------------
//课程分类

function switchCoursetype(coursetype) {
    switch (coursetype) {
        case "0":
            coursetype = "专业选修课";
            break;
        case "1":
            coursetype = "高新技术类";
            break;
        case "2":
            coursetype = "跨学科领域类";
            break;
        case "3":
            coursetype = "数学与自然科学类";
            break;
        case "4":
            coursetype = "人文与社会科学类";
            break;
        case "5":
            coursetype = "中华文明与外国文化类";
            break;
        case "6":
            coursetype = "礼仪修养与语言表达类";
            break;
        case "7":
            coursetype = "必修课";
    }
    return coursetype;
}